package per.xue.carid_booking.service.impl;

import per.xue.carid_booking.base.service.impl.BaseServiceImpl;
import per.xue.carid_booking.dto.CarMapperDTO;
import per.xue.carid_booking.entity.Car;
import per.xue.carid_booking.mapper.CarMapper;
import per.xue.carid_booking.service.CarService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-05-22
 */
@Service
public class CarServiceImpl extends BaseServiceImpl<CarMapper, Car> implements CarService {


    @Autowired
    private CarMapper carMapper;


    @Override
    /**
     * 当天的date使用new date
     * 之后的只要求精确到日
     */
    public List<Car> UserFindCar(Car car, Date date) throws Exception {
        //Date date = new Date();
        //获取后一天零点的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) date.clone());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(calendar.DATE,1);
        Date tomorrow = calendar.getTime();
        CarMapperDTO carMapperDTO = new CarMapperDTO(car, date, tomorrow);
        System.out.println(car);
        System.out.println(carMapperDTO);
        List<Car> carList = carMapper.findCar(carMapperDTO);
        return carList;
    }

    @Override
    /**
     * 只能精确到日
     */
    public List<Car> AdminFindCar(Car car,Date date ) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        System.out.println("Impl:car"+car);
        //获取当天零点的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        //Date date = calendar.getTime();
        calendar.add(calendar.DATE,1);
        Date tomorrow = calendar.getTime();
        System.out.println(date);
        System.out.println(tomorrow);
        CarMapperDTO carMapperDTO = new CarMapperDTO(car, date, tomorrow);
        List<Car> carList = carMapper.findCar(carMapperDTO);
        return carList;
    }

    @Override
    public List<Car> getPostCar(Date date) {
        return carMapper.getPostCar(date);
    }
}